﻿using ScottPlot;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UtilZ.Dotnet.Ex.Audio;
using UtilZ.Dotnet.Ex.Model;

namespace WinFormApp
{
    public partial class FWaveDemo : Form
    {
        public FWaveDemo()
        {
            InitializeComponent();
        }

        private void FWaveDemo_Load(object sender, EventArgs e)
        {

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            using OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.Filter = @"*.wav|*.wav";
            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            using var fileStream = File.OpenRead(openFileDialog.FileName);
            var waveInfo = WaveHelper.GetWavHead(fileStream);
            if (waveInfo == null)
            {
                return;
            }

            var headSize = WaveHelper.GetWaveHeadSize(waveInfo.Encoding);
            int length;
            double[] ys = null;
            using var bufferPtr = new UnmanagedMemoryPointer<byte>();
            fileStream.Position = headSize;

            switch (waveInfo.Encoding)
            {
                case WaveEncoding.MuLaw:
                case WaveEncoding.ALaw:
                    length = (int)((fileStream.Length - headSize));
                    bufferPtr.CheckSetLength(length);
                    fileStream.Read(bufferPtr.GetSpan());

                    SpanZ<byte> lawData = bufferPtr.Cast<byte>();
                    ys = new double[lawData.Length];
                    for (int i = 0; i < lawData.Length; i++)
                    {
                        ys[i] = lawData[i];
                    }
                    break;
                case WaveEncoding.Pcm:
                    length = (int)((fileStream.Length - headSize) / sizeof(short));
                    bufferPtr.CheckSetLength(length * sizeof(short));
                    fileStream.Read(bufferPtr.GetSpan());

                    SpanZ<short> pcmData = bufferPtr.Cast<short>();
                    ys = new double[pcmData.Length];
                    for (int i = 0; i < pcmData.Length; i++)
                    {
                        ys[i] = pcmData[i];
                    }
                    break;
                case WaveEncoding.IeeeFloat:
                    //headSize += 200;
                    length = (int)((fileStream.Length - headSize) / sizeof(float));
                    bufferPtr.CheckSetLength(length * sizeof(float));
                    fileStream.Read(bufferPtr.GetSpan());

                    SpanZ<float> pcm32Data = bufferPtr.Cast<float>();
                    ys = new double[pcm32Data.Length];
                    for (int i = 0; i < pcm32Data.Length; i++)
                    {
                        ys[i] = pcm32Data[i];
                    }

                    //for(int i=0;i<10;i++)
                    //{
                    //    ys[i] = 0;
                    //}
                    break;
                case WaveEncoding.Adpcm:
                    break;
                case WaveEncoding.IbmCvsd:
                    break;
                default:
                    return;
            }

            formsPlot1.Plot.Clear();
            formsPlot1.Plot.Add.Signal(ys);
        }
    }
}
